package com.nwpu.coursesupport.service.imple;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.ClassSearchCriteria;
import com.nwpu.coursesupport.entity.Class;
import com.nwpu.coursesupport.entity.Teacher;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.ClassType;
import com.nwpu.coursesupport.mapper.ClassMapper;
import com.nwpu.coursesupport.mapper.ClassStudentMapper;
import com.nwpu.coursesupport.mapper.CourseMapper;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.view.SCCView;
import com.nwpu.coursesupport.view.StudentGradeView;
import com.nwpu.coursesupport.view.criteria.StudentGradeDetailSearchCriteria;
import com.nwpu.coursesupport.view.criteria.StudentGradeViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.SCCViewMapper;
import com.nwpu.coursesupport.view.mapper.StudentGradeViewMapper;

@Service
public class StudentGradeViewService extends AbstractService{

	@Autowired
	private StudentGradeViewMapper studentGradeViewMapper;
	@Autowired
	private ClassMapper classMapper;
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private ClassStudentMapper classStudentMapper;
	@Autowired
	private SCCViewMapper sCCViewMapper;
	
    protected static Logger logger = LoggerFactory.getLogger(StudentGradeViewService.class);

	
	public JSONResult stuGradeDetail(IDParam p){
		JSONResult rst =new JSONResult();
		rst.fail();
		
		if(p==null||p.getId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		SCCView view= this.sCCViewMapper.findById(p.getId());
        
        if(view==null){
        	logger.error("该班级不存在");
        	return rst.fail("该班级不存在");
        }
		
		int id=(int) getSession().getAttribute("id");
		
		StudentGradeDetailSearchCriteria sc=new StudentGradeDetailSearchCriteria();
		sc.setClassId(p.getId());
		sc.setStudentId(id);
		
		StudentGradeView gradeView=this.studentGradeViewMapper.findByParam(sc);
		
		if(gradeView==null){
			logger.error("无该学生相关成绩信息");
			return rst.fail("无该学生相关成绩信息");
		}
		
		rst.addData("classView", view);
		rst.addData("gradeView", gradeView);
		rst.success();
		
		return rst;
		
	}
	
	
	public JSONResult list(StudentGradeViewSearchCriteria sc){
		JSONResult rst =new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getCourseId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		Teacher t=this.courseMapper.getTeacherByCourseId(sc.getCourseId());
		if(t==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		ClassSearchCriteria csc=new ClassSearchCriteria();
		csc.setClassType(ClassType.CLASS);
		logger.error("sh"+sc.getCourseId());
		csc.setCourseId(sc.getCourseId());
		List<Class> classes=this.classMapper.find(csc);
		
		List<String> classNumbers=this.classStudentMapper.findClassNumberByCourseId(sc.getCourseId());
		int id =(int) getSession().getAttribute("id");
		if(id!=t.getId()){
			logger.error("你无该权限");
			return rst.fail("你无该权限");
		}
		
        List<StudentGradeView> list=this.studentGradeViewMapper.find(sc);
        
        rst.addData("list", list);
        rst.addData("courseId",sc.getCourseId());
        rst.addData("classes", classes);
        rst.addData("classNumbers",classNumbers);
		rst.success();
		return rst;
	}
	
	public JSONResult listData(StudentGradeViewSearchCriteria sc){
		JSONResult rst =new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getCourseId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}

		Teacher t=this.courseMapper.getTeacherByCourseId(sc.getCourseId());
		if(t==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}

		int id =(int) getSession().getAttribute("id");
		if(id!=t.getId()){
			logger.error("你无该权限");
			return rst.fail("你无该权限");
		}
		
        List<StudentGradeView> list=this.studentGradeViewMapper.find(sc);
        
        rst.addData("list", list);
        rst.addData("courseId",sc.getCourseId());
		rst.success();
		return rst;
	}
	
	public List<StudentGradeView> gradeListData(IDParam p,JSONResult rst){
		
		if(p==null||p.getId()<=0){
			logger.error("参数有误");
			rst.fail("参数有误");
			return null;
		}
		
		Teacher t=this.classMapper.getTeacherByClassId(p.getId());
		if(t==null){
			logger.error("你没有该权限");
			rst.fail("你没有该权限");
			return null;
		}
		int id=(int) getSession().getAttribute("id");
		
		if(t.getId()!=id){
			logger.error("你无该权限");
			rst.fail("你无该权限");
			return null;
		}
		
		StudentGradeViewSearchCriteria sc=new StudentGradeViewSearchCriteria();
		sc.setClassId(p.getId());
		List<StudentGradeView> list=this.studentGradeViewMapper.find(sc);
		return list;
	} 
}
